import os
import shutil
import subprocess
import argparse

# ===== 配置区 =====
# 仓库根目录（请填写miao-plugin绝对路径，例如：r"C:/yunzai/miao-plugin" 或 "/home/yourname/miao-plugin"）
REPO_ROOT = r"miao-plugin"
# ==================

def delete_folder(folder_path):
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)
        print(f"已删除文件夹: {folder_path}")
    else:
        print(f"文件夹不存在: {folder_path}")

def get_main_branch(repo_path):
    # 获取主分支名
    result = subprocess.run(
        ["git", "symbolic-ref", "refs/remotes/origin/HEAD"],
        cwd=repo_path,
        capture_output=True,
        text=True
    )
    if result.returncode == 0:
        ref = result.stdout.strip()
        return ref.split("/")[-1]
    return "master"

def git_force_pull(repo_path):
    main_branch = get_main_branch(repo_path)
    subprocess.run(["git", "fetch", "--all"], cwd=repo_path)
    subprocess.run(["git", "reset", "--hard", f"origin/{main_branch}"], cwd=repo_path)
    print(f"已强制拉取并覆盖到 {main_branch} 分支")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="删除resources文件夹并强制git拉取覆盖")
    parser.add_argument(
        "--repo",
        type=str,
        default=REPO_ROOT,
        help="git仓库根目录"
    )
    args = parser.parse_args()

    print("即将删除 miao-plugin/resources 文件夹并强制拉取仓库覆盖")
    print("如需继续，请输入 ok 并回车，否则退出：")
    user_input = input().strip().lower()
    if user_input != "ok":
        print("操作已取消。")
        exit(0)

    resources_path = os.path.join(args.repo, "resources")
    delete_folder(resources_path)
    git_force_pull(args.repo)
